package shark.flow.identity;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.idm.api.*;
import org.flowable.idm.engine.IdmEngineConfiguration;
import org.flowable.idm.engine.impl.IdmIdentityServiceImpl;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import shark.flow.identity.impl.SharkGroupQueryImpl;
import shark.flow.identity.impl.SharkUserQueryImpl;
import shark.user.spi.entity.SysRole;
import shark.user.spi.entity.SysUser;
import shark.user.spi.mapper.SysRoleMapper;
import shark.user.spi.mapper.SysUserMapper;

import java.util.ArrayList;
import java.util.List;

public class SharkUserEntityManager extends IdmIdentityServiceImpl {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    public SharkUserEntityManager(IdmEngineConfiguration idmEngineConfiguration) {
        super(idmEngineConfiguration);
    }

    @Override
    public UserQuery createUserQuery() {
        return new SharkUserQueryImpl(sysUserMapper);
    }

    @Override
    public GroupQuery createGroupQuery() {
        return new SharkGroupQueryImpl(sysRoleMapper);
    }

    @Override
    public List<Group> getGroupsWithPrivilege(String name) {
        List<Group> groups = new ArrayList<>();
        List<SysRole> roles = sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>().like(SysRole::getRoleCode, name));
        for (SysRole role : roles) {
            GroupEntityImpl groupEntity = new GroupEntityImpl();
            groupEntity.setId(role.getId());
            groupEntity.setType(role.getRoleName());
            groupEntity.setName(role.getRoleCode());
            groups.add(groupEntity);
        }
        return groups;
    }

    @Override
    public List<User> getUsersWithPrivilege(String name) {
        List<User> users = new ArrayList<>();
        List<SysUser> userList = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, name));
        for (SysUser user : userList) {
            UserEntityImpl userEntity = new UserEntityImpl();
            userEntity.setId(user.getId());
            userEntity.setEmail(user.getEmail());
            userEntity.setPassword(user.getPassword());
            userEntity.setDisplayName(user.getUsername());
            userEntity.setLastName(user.getRealname());
            userEntity.setFirstName(user.getRealname());
            users.add(userEntity);
        }

        return users;
    }

    @Override
    public User newUser(String userId) {
        throw new FlowableException("LDAP identity service doesn't support creating a new user");
    }

    @Override
    public void saveUser(User user) {
        throw new FlowableException("LDAP identity service doesn't support saving an user");
    }

    @Override
    public NativeUserQuery createNativeUserQuery() {
        throw new FlowableException("LDAP identity service doesn't support native querying");
    }

    @Override
    public void deleteUser(String userId) {
        throw new FlowableException("LDAP identity service doesn't support deleting an user");
    }

    @Override
    public Group newGroup(String groupId) {
        throw new FlowableException("LDAP identity service doesn't support creating a new group");
    }

    @Override
    public NativeGroupQuery createNativeGroupQuery() {
        throw new FlowableException("LDAP identity service doesn't support native querying");
    }

    @Override
    public void saveGroup(Group group) {
        throw new FlowableException("LDAP identity service doesn't support saving a group");
    }

    @Override
    public void deleteGroup(String groupId) {
        throw new FlowableException("LDAP identity service doesn't support deleting a group");
    }
}
